import {arr2Tree} from "@/utils/tools.js";

export const menuTypes = [
    {value: 1, title: '目录', type: 'primary'},
    {value: 2, title: '菜单', type: 'success'},
    {value: 3, title: '按键', type: 'danger'},
    {value: 4, title: '外链', type: 'warning'},
];

export const getMenuType = (value) => {
    return menuTypes.find(menu => menu.value === value)
}

export const convertMenuToTree = (list) => {
    const map = new Map();
    list.forEach(item => {
        if (item.type === 2) {
            map.set(item['id'], {...item, btns: []});
        } else {
            map.set(item['id'], item);
        }
    });
    const data = [];
    map.forEach((item) => {
        if (item.type !== 3) {
            data.push(item);
        } else {
            if (item['pid'] > 0) {
                const parent = map.get(item['pid']);
                parent.btns.push(item);
            }
        }
    });
    return arr2Tree(data)
}